﻿<UserControl x:Class="FR.SAFTAnalyser.Views.InvoicesSummaryView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:conv="clr-namespace:FR.SAFTAnalyser.ValueConverters"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">

    <UserControl.Resources>
        <conv:GroupTotalSummaryConverter  x:Key="myConverter" />
        <CollectionViewSource x:Key="invoices" Source="{Binding Invoices, IsAsync=True}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Tipo"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>

        <CollectionViewSource x:Key="invoiceLines" Source="{Binding InvoiceLines, IsAsync=True}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Tipo"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="80"/>
            <RowDefinition Height="250"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        
        <!-- Total dos documentos -->
        <GroupBox Header="Totais">
            <Grid Grid.Row="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="30" />
                    <RowDefinition Height="30"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
            
                <TextBlock Text="Total débitos:" Grid.Row="0" Grid.Column="0" Foreground="{StaticResource Foreground}" Margin="5" Padding="3"/>
                <TextBlock Text="Total créditos:" Grid.Row="1" Grid.Column="0" Foreground="{StaticResource Foreground}" Margin="5" Padding="3"/>
                <TextBlock Text="{Binding TotalDebit, StringFormat=\{0:C\}}" Grid.Row="0" Grid.Column="1" Foreground="{StaticResource Foreground}" Margin="5" Padding="3"/>
                <TextBlock Text="{Binding TotalCredit, StringFormat=\{0:C\}}" Grid.Row="1" Grid.Column="1" Foreground="{StaticResource Foreground}" Margin="5" Padding="3"/>
            </Grid>
        </GroupBox>
        <!-- Total dos documentos -->
        <DockPanel Grid.Row="1">
            <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
                <RadioButton Content="Documentos por mês" GroupName="documentos" IsChecked="{Binding AgruparDocumentosMes}" Margin="3" Foreground="White" FontSize="12"/>
                <RadioButton Content="Documentos por dia" GroupName="documentos" IsChecked="{Binding AgruparDocumentosDia}" Margin="3" Foreground="White" FontSize="12"/>
                <Button Width="150" Height="25" Margin="5" Padding="3" ToolTip="Exportar para Excel" DockPanel.Dock="Bottom" 
                        Command="{Binding DoExportDocumentsCommand}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="..\Resources\excel.ico"/>
                        <TextBlock Text="Excel" Padding="3"/>
                    </StackPanel>
                </Button>
            </StackPanel>

            <DataGrid x:Name="dataGridInvoices" TabIndex="1" VirtualizingPanel.IsVirtualizingWhenGrouping="True" DockPanel.Dock="Bottom"
                      ItemsSource="{Binding Source={StaticResource invoices}}">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Tipo" Binding="{Binding Tipo}" />
                    <DataGridTextColumn Header="Data" Binding="{Binding Data, StringFormat=\{0:d\}}" />
                    <DataGridTextColumn Header="Total s/ IVA" Binding="{Binding Incidencia}" Width="100"/>
                    <DataGridTextColumn Header="Total" Binding="{Binding Total}" Width="70"/>
                    <DataGridTextColumn Header="Total imposto" Binding="{Binding Imposto}" Width="100"/>
                </DataGrid.Columns>
                
                <DataGrid.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding Path=Name}" />
                                </StackPanel>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Expander IsExpanded="False">
                                                <Expander.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock Text="{Binding Path=Name}" Foreground="{StaticResource Foreground}"/>
                                                        <TextBlock Text=" ( " Foreground="{StaticResource Foreground}"/>
                                                        <TextBlock Text="{Binding Path=ItemCount}" Foreground="{StaticResource Foreground}"/>
                                                        <TextBlock Text=" ) " Foreground="{StaticResource Foreground}"/>
                                                        <TextBlock Text="{Binding Path=Items, Converter={StaticResource myConverter}}" Foreground="{StaticResource Foreground}" HorizontalAlignment="Right"/>
                                                    </StackPanel>
                                                </Expander.Header>
                                                <ItemsPresenter />
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </DataGrid.GroupStyle>

            </DataGrid>
        </DockPanel>

        <!-- Total das linhas -->
        <DockPanel Grid.Row="2">
            <StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
                <RadioButton Content="Linhas por código" GroupName="linhas" IsChecked="{Binding AgruparLinhasPorCodigo}" Margin="3" Foreground="White" FontSize="12"/>
                <RadioButton Content="Linhas por descrição" GroupName="linhas" IsChecked="{Binding AgruparLinhasPorDescricao}" Margin="3" Foreground="White" FontSize="12"/>
                <Button Width="150" Height="25" Margin="5" Padding="3" ToolTip="Exportar para Excel" DockPanel.Dock="Bottom" 
                        Command="{Binding DoExportlinesCommand}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="..\Resources\excel.ico"/>
                        <TextBlock Text="Excel" Padding="3"/>
                    </StackPanel>
                </Button>
            </StackPanel>

            <DataGrid x:Name="dataGridInvoiceLines" TabIndex="1" DockPanel.Dock="Bottom"
                      ItemsSource="{Binding Source={StaticResource invoiceLines}}">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Tipo" Binding="{Binding Tipo}" />
                    <DataGridTextColumn Header="Produto" Binding="{Binding Produtos}" />
                    <DataGridTextColumn Header="Total s/ IVA" Binding="{Binding Incidencia}" Width="90"/>
                    <DataGridTextColumn Header="Imposto" Binding="{Binding TaxaImposto}" />
                </DataGrid.Columns>

                <DataGrid.GroupStyle>
                    <GroupStyle>
                        <GroupStyle.HeaderTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding Path=Name}" />
                                </StackPanel>
                            </DataTemplate>
                        </GroupStyle.HeaderTemplate>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Expander IsExpanded="False">
                                                <Expander.Header>
                                                    <StackPanel Orientation="Horizontal">
                                                        <TextBlock Text="{Binding Path=Name}" Foreground="{StaticResource Foreground}"/>
                                                        <TextBlock Text=" ( " Foreground="{StaticResource Foreground}"/>
                                                        <TextBlock Text="{Binding Path=ItemCount}" Foreground="{StaticResource Foreground}"/>
                                                        <TextBlock Text=" ) " Foreground="{StaticResource Foreground}"/>
                                                        <TextBlock Text="{Binding Path=Items, Converter={StaticResource myConverter}}" Foreground="{StaticResource Foreground}" HorizontalAlignment="Right"/>
                                                    </StackPanel>
                                                </Expander.Header>
                                                <ItemsPresenter />
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </DataGrid.GroupStyle>
                
            </DataGrid>
        </DockPanel>
    </Grid>
</UserControl>
